﻿@using PureLib.Common
@using WhereAreThem.Model.Models
@using WhereAreThem.WebViewer.Models
@model Folder
@{
    string machineName = ViewBag.MachineName;
    List<Folder> stack = ViewBag.Stack;
    ViewBag.Title = (stack == null) ? Extensions.ActionExplorer : Extensions.ActionExplorer.GetTitle(Model.GetFullPath(stack), machineName);
    const int rowCount = 15;
}

@using (Html.BeginForm()) {
    <div class="header">
        @if (stack != null) {
            @Html.TextBox("searchPattern", null, new { @class = "search" })
        }
        <div class="navigation">
            <div class="machineName">
                <a href="@Url.Action(Extensions.ActionExplorer, new { machineName = machineName })">@machineName</a>
            </div>
            @if (stack != null) {
                for (int i = 0; i < stack.Count; i++) {
                    <div class="navigationItem">
                        <span id="@i" class="icon separator"></span>
                    </div>
                    <div class="navigationItem">
                        <a href="@Url.Action(Extensions.ActionExplorer, new { machineName = machineName, path = stack[i].GetFullPath(stack) })">
                            <span class="icon folder item iconText">@stack[i].Name</span>
                        </a>
                    </div>
                    if (i > 0) {
                        <ul id="@string.Format("stack_{0}", i)" class="contextMenu">
                            @foreach (Folder f in stack[i - 1].Folders) {
                                <li class="@(f == stack[i] ? "current" : string.Empty)">
                                    <a href="@Url.Action(Extensions.ActionExplorer, new { machineName = machineName, path = f.GetFullPath(stack.Take(i).ToList()) })">
                                        <span class="icon folder item iconText">@f.Name</span>
                                    </a>
                                </li>
                            }
                        </ul>
                    }
                }
            }
        </div>
    </div>
}

<div class="explorerContainer">
    @if (Session.GetExplorerView() == ExplorerView.List) {
        int count = 0;
        <table class="viewer explorer list"><tbody>
        @if (Model.Folders != null) {
            foreach (Folder f in Model.Folders) {
                if ((count > 0) && ((count % rowCount) == 0)) {
                    @Html.Raw("</tbody></table><table class=\"viewer explorer list\"><tbody>")
                }
                <tr>
                    <td>
                        <a href="@Url.Action(Extensions.ActionExplorer, new { machineName = machineName, path = stack == null ? f.Name : f.GetFullPath(stack) })">
                            <span class="icon folder item iconText">@f.Name</span>
                        </a>
                    </td>
                </tr>
                count++;
            }
        }
        @if (Model.Files != null) {
            foreach (WhereAreThem.Model.Models.File f in Model.Files) {
                if ((count > 0) && ((count % rowCount) == 0)) {
                    @Html.Raw("</tbody></table><table class=\"viewer explorer list\"><tbody>")
                }
                <tr>
                    <td>
                        <a href="#">
                            <span class="icon file item iconText">@f.Name</span>
                        </a>
                    </td>
                </tr>
                count++;
            }
        }
        </tbody></table>
    }
    else if (Session.GetExplorerView() == ExplorerView.Details) {
        <table class="viewer explorer">
            <thead>
                <tr>
                    <th class="name">Name</th>
                    <th class="size">Size</th>
                    <th class="datetime">Created</th>
                    <th class="datetime">Modified</th>
                </tr>
            </thead>
            <tbody>
                @if (Model.Folders == null && Model.Files == null) {
                    <tr>
                        <td colspan="4"><div class="empty">Empty</div></td>
                    </tr>
                }
                @if (Model.Folders != null) {
                    foreach (Folder f in Model.Folders) {
                    <tr>
                        <td>
                            <a href="@Url.Action(Extensions.ActionExplorer, new { machineName = machineName, path = stack == null ? f.Name : f.GetFullPath(stack) })">
                                <span class="icon folder item iconText">@f.Name</span>
                            </a>
                        </td>
                        <td class="size nowrap">@f.Size.ToFriendlyString()</td>
                        <td class="nowrap">@f.CreatedDate.ToExplorerString()</td>
                        <td></td>
                    </tr>
                    }
                }
                @if (Model.Files != null) {
                    foreach (WhereAreThem.Model.Models.File f in Model.Files) {
                    <tr>
                        <td>
                            <a href="#">
                                <span class="icon file item iconText">@f.Name</span>
                            </a>
                        </td>
                        <td class="size nowrap">@f.Size.ToFriendlyString()</td>
                        <td class="nowrap">@f.CreatedDate.ToExplorerString()</td>
                        <td class="nowrap">@f.ModifiedDate.ToExplorerString()</td>
                    </tr>
                    }
                }
            </tbody>
        </table>
    }
</div>

<ul id="explorerContextMenu" class="contextMenu">
    <li>
        <a href="#open">
            <span class="icon enter item iconText">Open</span>
        </a>
    </li>
    <li>
        <a href="#properties">
            <span class="icon properties item iconText">Properties</span>
        </a>
    </li>
</ul>
